import axios from 'axios'
import router from '../router'
import {Toast} from 'vant'

//注册接口
export const reqregister = (user) =>{
    return axios({

        url:"/api/register",
        method:"post",
        data:user,
    })
}
//登录接口
export const reqlogin = (user) =>{
    return axios({

        url:"/api/login",
        method:"post",
        data:user,
    })
}




//首页的轮播
export const reqhomebanner = () =>{
    return axios({

        url:"/api/getbanner",
        method:"get",
    })
}
//首页的分类
export const reqhomecate = () =>{
    return  axios({
        url:'/api/getcate',
        method:'get',
    })
}
//首页的商品
export const reqhomegoods = ()=>{
    return axios({
        url: "/api/gethortgoods",
        method: "get",
      })
}
//搜索
export const reqsearch = (user)=>{
    return axios({
        url: "/api/search",
        method: "get",
        params:user
      })
}
// 分类
export const reqcate=()=>{
    return axios({
        url:"/api/getcates",
        method:"get"
    })
}

//商品列表
export const reqlist = (user)=>{
    return axios({
        url:'/api/getgoodlist',
        method:'get',
        params:user,
       
    })
}
//商品详情
export const reqdetail = (user) => {
    return axios({
        url: "/api/getgoodsinfo",
        method: "get",
        params: user
    })
}
//购物车添加
export const reqcartadd = (user) => {
    return axios({
        url: "/api/cartadd",
        method: "post",
        data: user,
        // headers: {
        //     authorization: JSON.parse(localStorage.getItem("isLogin")).token
        // }
    })
}
//购物车列表
export const reqcartlist = (user) => {
    return axios({
        url: "/api/cartlist",
        method: "get",
        params:user,
        // headers: {
        //     authorization: JSON.parse(localStorage.getItem("isLogin")).token
        // }
    })
}
//购物车修改
export const reqcartedit=(user)=>{
    return axios({
        url:"/api/cartedit",
        method:"post",
        data:user,
        // headers: {
        //     authorization: JSON.parse(localStorage.getItem("isLogin")).token
        // }
    })
}
//购物车删除
export const reqcartdel=(user)=>{
    return axios({
        url:"/api/cartdelete",
        method:"post",
        data:user,
        // headers: {
        //     authorization: JSON.parse(localStorage.getItem("isLogin")).token
        // }
    })
}
//请求拦截
axios.interceptors.request.use(config=>{
    if(config.url !='/api/register' && config.url !='/api/login'){
        config.headers.authorization = JSON.parse(localStorage.getItem("isLogin")).token;
    }
    return config
})
//响应拦截
axios.interceptors.response.use(res=>{
    console.group('本次请求完成,请求地址是:'+res.config.url);
    console.log(res);
    console.groupEnd();
    //统一处理失败
    if(res.data.code!=200){
        alert(res.data.msg)
    }
    //统一处理掉线情况
    if(res.data.msg==="登录已过期或访问权限受限"){
        //跳转到登录
       router.replace("/login")
    }
    return res;  
})
//地址添加
export const reqaddressadd =(p)=>{
    return axios({
        url:"/api/addressadd",
        method:"post",
        data:p
    })
}

// 地址删除
export const reqaddressremove =(p)=>{
    return axios({
        url:"/api/addressremove",
        method:"post",
        data:p
    })
}

//地址编辑
export const reqaddressedit =(p)=>{
    return axios({
        url:"/api/addressedit",
        method:"post",
        data:p
    })
}
//地址列表
export const reqaddresslist =(p)=>{
    return axios({
        url:"/api/addresslist",
        method:"get",
        params:p
    })
}
//订单生成
export const reqorderadd =(p)=>{
    return axios({
        url:"/api/orderadd",
        method:"post",
        data:p
    })
}








